package com.example.util;



import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class FreemarkerTest {
    private static String document= "document.xml";
    //outputStream 输出流可以自己定义 浏览器或者文件输出流
    public static void createDocx(Map dataMap, OutputStream outputStream) {
        ZipOutputStream zipout = null;
        try {
            /*//图片配置文件模板
            ByteArrayInputStream documentXmlRelsInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, documentXmlRels);*/

            //内容模板
            ByteArrayInputStream documentInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, document);
            //最初设计的模板
            //File docxFile = new File(WordUtils.class.getClassLoader().getResource(template).getPath());
            File docxFile = new File("C:\\Users\\dyb\\Desktop\\test\\123.zip");//换成自己的zip路径

            if (!docxFile.exists()) {
                System.out.println(111111);
                docxFile.createNewFile();
            }
            ZipFile zipFile = new ZipFile(docxFile);
            Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
            zipout = new ZipOutputStream(outputStream);
            //开始覆盖文档------------------
            int len = -1;
            byte[] buffer = new byte[1024];
            while (zipEntrys.hasMoreElements()) {
                ZipEntry next = zipEntrys.nextElement();
                InputStream is = zipFile.getInputStream(next);
                if (next.toString().indexOf("media") < 0) {
                    zipout.putNextEntry(new ZipEntry(next.getName()));
                    if ("word/document.xml".equals(next.getName())) {//如果是word/document.xml由我们输入
                        if (documentInput != null) {
                            while ((len = documentInput.read(buffer)) != -1) {
                                zipout.write(buffer, 0, len);
                            }
                            documentInput.close();
                        }
                    } else {
                        while ((len = is.read(buffer)) != -1) {
                            zipout.write(buffer, 0, len);
                        }
                        is.close();
                    }
                }
            }

        } catch (Exception e) {
            System.out.println("word导出失败:"+e.getStackTrace());
            //logger.error();
        }finally {
            if(zipout!=null){
                try {
                    zipout.close();
                } catch (IOException e) {
                    System.out.println("io异常");

                }
            }
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    System.out.println("io异常");
                }
            }
        }
    }
    public static void main(String arg[]){
        Map dataMap=new HashMap();


        dataMap.put("meeting_name", "如何使象牙山发展得更加美好");
        dataMap.put("time", "2019-09-15 15:30");
        dataMap.put("site", "会议室212");
        dataMap.put("organizer", "张三");
        dataMap.put("department", "策划部");
        dataMap.put("attendee", "谢大脚、谢广坤、刘能");
        dataMap.put("meeting_content", "关于象牙山发展中的每个人的义务");
        dataMap.put("recorder", "王五");
        dataMap.put("checker", "大老板");
        dataMap.put("jueyi", "关于象牙山发展中的每个人的义务的决议");



        //指定输出docx路径
        File outFile = new File("C:\\Users\\dyb\\Desktop\\test\\test1.docx") ;
        try {
            createDocx(dataMap,new FileOutputStream(outFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}